package com.zwl.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zwl.dao.BaseDao;
import com.zwl.dao.MenuDao;
import com.zwl.po.MenuPo;
import com.zwl.po.PageBean;

public class MenuDaoImpl extends BaseDao<MenuPo> implements MenuDao {

	@Override
	public List<MenuPo> findByCondition(Map<String, Object> map) {
		List<Object> paramList=new ArrayList<>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from b_menu where 1=1 ");

		if(map!=null && map.size()>0){
			if(map.get("menuId")!=null){
				sf.append(" and menu_id=? ");
				paramList.add(map.get("menuId"));
			}
			
			if(map.get("exincludeMenuId")!=null){
				sf.append(" and menu_id<>? ");
				paramList.add(map.get("exincludeMenuId"));
			}
		}
		
		Map<String, String> columnsMap=new HashMap<String, String>();
		columnsMap.put("menu_id", "id");
		return this.executeQuery3(sf.toString(), columnsMap, MenuPo.class, paramList.toArray());
	}
	
	@Override
	public MenuPo findbyId(Integer id) {
		String sql="select * from b_menu where menu_id=?";
		Map<String, String> columnsMap=new HashMap<String, String>();
		columnsMap.put("menu_id", "id");
		List<MenuPo> list=this.executeQuery3(sql, columnsMap, MenuPo.class, id);
		return (list!=null && list.size()>0)?list.get(0):null;
	}

	@Override
	public MenuPo getEntity(ResultSet rs) {
		return null;
	}

	@Override
	public Integer[] saveMenu(MenuPo entity) {
		String sql="insert into b_menu values(seq_menu_id.nextval,?,?,?,?,?,?,?,?,?,?)";
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(entity.getMenuName());
		paramList.add(entity.getMenuUrl());
		paramList.add(entity.getMenuIcon());
		paramList.add(entity.getMenuOrder());
		paramList.add(entity.getMenuParentId());
		paramList.add(entity.getMenuIsactive());
		paramList.add(entity.getCreatedId());
		paramList.add(entity.getCreatedTime());
		paramList.add(entity.getUpdatedId());
		paramList.add(entity.getUpdatedTime());
		return (Integer[]) this.executeUpdate(sql,"insert",new String[]{"MENU_ID"}, paramList.toArray());
	}

	@Override
	public int updateMenu(MenuPo entity) {
		StringBuffer sf=new StringBuffer();
		sf.append(" update b_menu set ");
		sf.append("    menu_name=? ");
		sf.append("   ,menu_url=? ");
		sf.append("   ,menu_icon=? ");
		sf.append("   ,menu_order=? ");
		sf.append("   ,menu_parent_id=? ");
		sf.append("   ,menu_isactive=? ");
		sf.append("   ,created_Id=? ");
		sf.append("   ,created_Time=? ");
		sf.append("   ,updated_Id=? ");
		sf.append("   ,updated_Time=? ");
		sf.append(" where menu_id=? ");
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(entity.getMenuName());
		paramList.add(entity.getMenuUrl());
		paramList.add(entity.getMenuIcon());
		paramList.add(entity.getMenuOrder());
		paramList.add(entity.getMenuParentId());
		paramList.add(entity.getMenuIsactive());
		paramList.add(entity.getCreatedId());
		paramList.add(entity.getCreatedTime());
		paramList.add(entity.getUpdatedId());
		paramList.add(entity.getUpdatedTime());
		paramList.add(entity.getId());
		return (Integer) this.executeUpdate(sf.toString(),"update",null, paramList.toArray());
	}

	@Override
	public PageBean<MenuPo> findPageByCondition(int pageIndex, int pageSize, Map<String, Object> map) {
		PageBean<MenuPo> pagebean=new PageBean<MenuPo>();
		pagebean.setPageIndex(pageIndex);
		pagebean.setPageSize(pageSize);
		List<Object> paramList=new ArrayList<Object>();
		
		
		StringBuffer baseSql=new StringBuffer();
		baseSql.append(" select * from b_menu where 1=1 ");
		
		
		pagebean.setTotalCount(this.getCount(baseSql.toString(), paramList.toArray()));
		
		if(pagebean.getTotalCount()>0){
			StringBuffer pageSql=new StringBuffer();
			pageSql.append(" select * from ( ");
			pageSql.append("  select tmp1.*,rownum rn from ( ");
			pageSql.append(baseSql.toString());
			pageSql.append("  ) tmp1");
			pageSql.append(" ) tmp2");
			pageSql.append(" where tmp2.rn>? and tmp2.rn<=? ");
			int startRow=(pagebean.getPageIndex()-1)*pagebean.getPageSize();
			int endRow=pagebean.getPageIndex()*pagebean.getPageSize();
			paramList.add(startRow);
			paramList.add(endRow);
			
			Map<String, String> columnsMap=new HashMap<String, String>();
			columnsMap.put("menu_id", "id");
			pagebean.setList(this.executeQuery3(pageSql.toString(), columnsMap, MenuPo.class, paramList.toArray()));
		}
		
		return pagebean;
	}

}
